<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人信息 - Asurada SSO客户端</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        .profile-header {
            background: linear-gradient(135deg, #6f42c1 0%, #e83e8c 100%);
            color: white;
            padding: 60px 0;
            margin-bottom: 40px;
        }
        .profile-avatar {
            width: 120px;
            height: 120px;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 4rem;
            margin: 0 auto 20px;
        }
        .info-card {
            border: none;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease;
        }
        .info-card:hover {
            transform: translateY(-2px);
        }
        .info-label {
            font-weight: 600;
            color: #6c757d;
            margin-bottom: 5px;
        }
        .info-value {
            font-size: 1.1rem;
            color: #212529;
            margin-bottom: 20px;
        }
        .security-section {
            background: linear-gradient(135deg, #17a2b8 0%, #20c997 100%);
            color: white;
            border-radius: 15px;
            padding: 30px;
        }
        .activity-item {
            border-left: 3px solid #007bff;
            padding-left: 15px;
            margin-bottom: 20px;
        }
        .badge-custom {
            background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
            color: white;
            padding: 8px 16px;
            border-radius: 20px;
            font-size: 0.9rem;
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" href="/">
                <i class="bi bi-shield-check"></i>
                Asurada SSO客户端
            </a>
            
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="/">首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/protected">受保护页面</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="/profile">个人信息</a>
                    </li>
                </ul>
                
                <ul class="navbar-nav">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown">
                            <i class="bi bi-person-circle"></i>
                            <span th:text="${user?.username} ?: '用户'">用户</span>
                        </a>
                        <ul class="dropdown-menu">
                            <li><a class="dropdown-item" href="/profile">
                                <i class="bi bi-person"></i> 个人信息
                            </a></li>
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item" href="#" onclick="logout()">
                                <i class="bi bi-box-arrow-right"></i> 登出
                            </a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- 主要内容 -->
    <main>
        <!-- 页面头部 -->
        <section class="profile-header">
            <div class="container">
                <div class="row align-items-center">
                    <div class="col-lg-4 text-center">
                        <div class="profile-avatar">
                            <i class="bi bi-person-fill"></i>
                        </div>
                        <h2 th:text="${user.username}">用户名</h2>
                        <p class="mb-0 opacity-75">SSO认证用户</p>
                    </div>
                    <div class="col-lg-8">
                        <h1 class="display-5 fw-bold mb-3">个人信息中心</h1>
                        <p class="lead mb-4">
                            管理您的个人信息和账户设置。所有信息通过SSO服务端统一管理，
                            确保在所有客户端应用中保持一致。
                        </p>
                        <div class="d-flex align-items-center">
                            <span class="badge-custom me-3">
                                <i class="bi bi-shield-check me-1"></i>
                                已认证
                            </span>
                            <span class="badge-custom">
                                <i class="bi bi-clock me-1"></i>
                                在线
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <div class="container">
            <!-- 基本信息 -->
            <div class="row mb-5">
                <div class="col-lg-8">
                    <div class="card info-card">
                        <div class="card-header bg-primary text-white">
                            <h5 class="mb-0">
                                <i class="bi bi-person-lines-fill me-2"></i>
                                基本信息
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="info-label">用户ID</div>
                                    <div class="info-value" th:text="${user.id}">1</div>
                                    
                                    <div class="info-label">用户名</div>
                                    <div class="info-value" th:text="${user.username}">testuser</div>
                                </div>
                                <div class="col-md-6">
                                    <div class="info-label">邮箱地址</div>
                                    <div class="info-value" th:text="${user.email}">user@example.com</div>
                                    
                                    <div class="info-label">手机号码</div>
                                    <div class="info-value" th:text="${user.mobile}">13800138000</div>
                                </div>
                            </div>
                            
                            <div class="alert alert-info mt-3">
                                <i class="bi bi-info-circle me-2"></i>
                                <strong>提示：</strong>个人信息由SSO服务端统一管理，如需修改请联系系统管理员。
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="col-lg-4">
                    <div class="card info-card">
                        <div class="card-header bg-success text-white">
                            <h5 class="mb-0">
                                <i class="bi bi-shield-check me-2"></i>
                                账户状态
                            </h5>
                        </div>
                        <div class="card-body text-center">
                            <div class="mb-3">
                                <i class="bi bi-check-circle-fill text-success" style="font-size: 3rem;"></i>
                            </div>
                            <h6 class="text-success">账户正常</h6>
                            <p class="text-muted mb-3">您的账户状态良好，所有功能正常可用。</p>
                            
                            <div class="d-grid gap-2">
                                <button class="btn btn-outline-primary btn-sm" onclick="refreshUserInfo()">
                                    <i class="bi bi-arrow-clockwise me-1"></i>
                                    刷新信息
                                </button>
                                <button class="btn btn-outline-secondary btn-sm" onclick="checkAccountSecurity()">
                                    <i class="bi bi-shield-lock me-1"></i>
                                    安全检查
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 安全信息 -->
            <div class="security-section mb-5">
                <h3 class="mb-4">
                    <i class="bi bi-shield-lock me-2"></i>
                    安全信息
                </h3>
                <div class="row">
                    <div class="col-md-4 text-center mb-3">
                        <i class="bi bi-key" style="font-size: 2.5rem;"></i>
                        <h5 class="mt-2">JWT认证</h5>
                        <p class="mb-0">使用安全的JWT令牌进行身份认证</p>
                    </div>
                    <div class="col-md-4 text-center mb-3">
                        <i class="bi bi-clock-history" style="font-size: 2.5rem;"></i>
                        <h5 class="mt-2">会话管理</h5>
                        <p class="mb-0">令牌有效期30分钟，支持自动刷新</p>
                    </div>
                    <div class="col-md-4 text-center mb-3">
                        <i class="bi bi-shield-check" style="font-size: 2.5rem;"></i>
                        <h5 class="mt-2">单点登录</h5>
                        <p class="mb-0">统一身份认证，安全便捷</p>
                    </div>
                </div>
            </div>

            <!-- 最近活动 -->
            <div class="row mb-5">
                <div class="col-lg-8">
                    <div class="card info-card">
                        <div class="card-header bg-info text-white">
                            <h5 class="mb-0">
                                <i class="bi bi-clock-history me-2"></i>
                                最近活动
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="activity-item">
                                <div class="d-flex justify-content-between align-items-start">
                                    <div>
                                        <h6 class="mb-1">
                                            <i class="bi bi-box-arrow-in-right text-success me-2"></i>
                                            成功登录
                                        </h6>
                                        <p class="text-muted mb-0">通过SSO服务端认证登录系统</p>
                                    </div>
                                    <small class="text-muted" id="loginTime">刚刚</small>
                                </div>
                            </div>
                            
                            <div class="activity-item">
                                <div class="d-flex justify-content-between align-items-start">
                                    <div>
                                        <h6 class="mb-1">
                                            <i class="bi bi-shield-check text-primary me-2"></i>
                                            访问受保护页面
                                        </h6>
                                        <p class="text-muted mb-0">令牌验证通过，成功访问受保护资源</p>
                                    </div>
                                    <small class="text-muted">几分钟前</small>
                                </div>
                            </div>
                            
                            <div class="activity-item">
                                <div class="d-flex justify-content-between align-items-start">
                                    <div>
                                        <h6 class="mb-1">
                                            <i class="bi bi-person text-info me-2"></i>
                                            查看个人信息
                                        </h6>
                                        <p class="text-muted mb-0">访问个人信息页面</p>
                                    </div>
                                    <small class="text-muted">现在</small>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="col-lg-4">
                    <div class="card info-card">
                        <div class="card-header bg-warning text-dark">
                            <h5 class="mb-0">
                                <i class="bi bi-gear me-2"></i>
                                快速操作
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="d-grid gap-2">
                                <button class="btn btn-primary" onclick="testTokenValidation()">
                                    <i class="bi bi-shield-check me-2"></i>
                                    验证令牌
                                </button>
                                <button class="btn btn-success" onclick="refreshToken()">
                                    <i class="bi bi-arrow-repeat me-2"></i>
                                    刷新令牌
                                </button>
                                <button class="btn btn-info" onclick="getUserInfo()">
                                    <i class="bi bi-person me-2"></i>
                                    获取用户信息
                                </button>
                                <hr>
                                <a href="/protected" class="btn btn-outline-primary">
                                    <i class="bi bi-shield-lock me-2"></i>
                                    受保护页面
                                </a>
                                <a href="/" class="btn btn-outline-secondary">
                                    <i class="bi bi-house me-2"></i>
                                    返回首页
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 操作结果显示 -->
            <div id="operationResult" class="mb-5" style="display: none;">
                <div class="card">
                    <div class="card-header bg-secondary text-white">
                        <h5 class="mb-0">
                            <i class="bi bi-terminal me-2"></i>
                            操作结果
                        </h5>
                    </div>
                    <div class="card-body">
                        <pre id="resultContent" class="mb-0"></pre>
                    </div>
                </div>
            </div>

            <!-- 系统信息 -->
            <div class="row">
                <div class="col-12">
                    <div class="card info-card">
                        <div class="card-header bg-dark text-white">
                            <h5 class="mb-0">
                                <i class="bi bi-info-square me-2"></i>
                                系统信息
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-3">
                                    <div class="info-label">客户端版本</div>
                                    <div class="info-value">v1.0.0</div>
                                </div>
                                <div class="col-md-3">
                                    <div class="info-label">Java版本</div>
                                    <div class="info-value">JDK 21</div>
                                </div>
                                <div class="col-md-3">
                                    <div class="info-label">Spring Boot</div>
                                    <div class="info-value">3.5.3</div>
                                </div>
                                <div class="col-md-3">
                                    <div class="info-label">认证方式</div>
                                    <div class="info-value">SSO + JWT</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <!-- 页脚 -->
    <footer class="bg-light py-4 mt-5">
        <div class="container">
            <div class="row">
                <div class="col-md-6">
                    <p class="text-muted mb-0">
                        &copy; 2024 Asurada SSO客户端演示应用
                    </p>
                </div>
                <div class="col-md-6 text-end">
                    <p class="text-muted mb-0">
                        个人信息页面 - 安全访问
                    </p>
                </div>
            </div>
        </div>
    </footer>

    <!-- JavaScript -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    
    <script>
        // 登出函数
        function logout() {
            if (confirm('确定要登出吗？')) {
                axios.post('/sso/logout')
                    .then(function (response) {
                        if (response.data.code === 200) {
                            window.location.href = '/';
                        } else {
                            alert('登出失败：' + response.data.message);
                        }
                    })
                    .catch(function (error) {
                        console.error('登出请求失败:', error);
                        alert('登出失败，请重试');
                    });
            }
        }
        
        // 刷新用户信息
        function refreshUserInfo() {
            showResult('正在刷新用户信息...');
            
            axios.get('/sso/userinfo')
                .then(function (response) {
                    if (response.data.code === 200) {
                        const result = {
                            status: '刷新成功',
                            userInfo: response.data.data,
                            timestamp: new Date().toLocaleString()
                        };
                        showResult(JSON.stringify(result, null, 2));
                        
                        // 可以选择刷新页面显示最新信息
                        setTimeout(() => {
                            if (confirm('用户信息已刷新，是否重新加载页面显示最新信息？')) {
                                location.reload();
                            }
                        }, 2000);
                    } else {
                        showResult('刷新失败: ' + response.data.message);
                    }
                })
                .catch(function (error) {
                    showResult('刷新失败: ' + error.message);
                });
        }
        
        // 检查账户安全
        function checkAccountSecurity() {
            showResult('正在进行安全检查...');
            
            axios.get('/sso/check')
                .then(function (response) {
                    const securityInfo = {
                        tokenValid: response.data.code === 200 && response.data.data,
                        authMethod: 'SSO + JWT',
                        sessionSecure: true,
                        lastCheck: new Date().toLocaleString(),
                        recommendations: [
                            '定期更换密码',
                            '不要在公共设备上保持登录',
                            '及时登出系统'
                        ]
                    };
                    showResult(JSON.stringify(securityInfo, null, 2));
                })
                .catch(function (error) {
                    showResult('安全检查失败: ' + error.message);
                });
        }
        
        // 测试令牌验证
        function testTokenValidation() {
            showResult('正在验证令牌...');
            
            axios.get('/sso/check')
                .then(function (response) {
                    const result = {
                        operation: '令牌验证',
                        valid: response.data.code === 200 && response.data.data,
                        message: response.data.message,
                        timestamp: new Date().toLocaleString()
                    };
                    showResult(JSON.stringify(result, null, 2));
                })
                .catch(function (error) {
                    showResult('验证失败: ' + error.message);
                });
        }
        
        // 刷新令牌
        function refreshToken() {
            showResult('正在刷新令牌...');
            
            axios.post('/sso/refresh')
                .then(function (response) {
                    if (response.data.code === 200) {
                        const result = {
                            operation: '令牌刷新',
                            status: '成功',
                            newToken: '已获取新令牌',
                            expiresIn: response.data.data.expiresIn + '秒',
                            timestamp: new Date().toLocaleString()
                        };
                        showResult(JSON.stringify(result, null, 2));
                    } else {
                        showResult('刷新失败: ' + response.data.message);
                    }
                })
                .catch(function (error) {
                    showResult('刷新失败: ' + error.message);
                });
        }
        
        // 获取用户信息
        function getUserInfo() {
            showResult('正在获取用户信息...');
            
            axios.get('/sso/userinfo')
                .then(function (response) {
                    if (response.data.code === 200) {
                        const result = {
                            operation: '获取用户信息',
                            status: '成功',
                            data: response.data.data,
                            timestamp: new Date().toLocaleString()
                        };
                        showResult(JSON.stringify(result, null, 2));
                    } else {
                        showResult('获取失败: ' + response.data.message);
                    }
                })
                .catch(function (error) {
                    showResult('获取失败: ' + error.message);
                });
        }
        
        // 显示操作结果
        function showResult(content) {
            const resultDiv = document.getElementById('operationResult');
            const resultContent = document.getElementById('resultContent');
            
            resultContent.textContent = content;
            resultDiv.style.display = 'block';
            
            // 滚动到结果区域
            resultDiv.scrollIntoView({ behavior: 'smooth' });
        }
        
        // 页面加载完成后的初始化
        document.addEventListener('DOMContentLoaded', function() {
            // 更新登录时间显示
            document.getElementById('loginTime').textContent = '刚刚';
            
            // 定期检查登录状态
            setInterval(function() {
                axios.get('/sso/check')
                    .then(function (response) {
                        if (response.data.code === 200 && !response.data.data) {
                            if (confirm('您的登录已过期，是否重新登录？')) {
                                window.location.href = '/login?redirect=' + encodeURIComponent(window.location.pathname);
                            }
                        }
                    })
                    .catch(function (error) {
                        console.error('检查登录状态失败:', error);
                    });
            }, 5 * 60 * 1000); // 5分钟检查一次
        });
    </script>
</body>
</html>